package com.pxst.fo.sys.controller;

import com.pxst.exception.BusinessException;
import com.pxst.fo.sys.req.FoOrderMainReq;
import com.pxst.fo.sys.resp.FoOrderMainDownloadResp;
import com.pxst.fo.sys.resp.FoOrderMainResp;
import com.pxst.fo.sys.service.FoOrderMainService;
import com.pxst.resp.ApiResp;
import com.pxst.resp.PageResp;
import com.pxst.utils.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api/fo")
@Api(tags = "代付相关接口")
public class FoOrderMainController {
    @Autowired
    private FoOrderMainService foOrderMainService;

    @GetMapping("/list")
    @ApiOperation("搜索接口")
    public ApiResp<PageResp<FoOrderMainResp>> selectPage(FoOrderMainReq foOrderMainReq){
        return foOrderMainService.selectPage(foOrderMainReq);
    }

    @GetMapping("/download")
    @ApiOperation("汇出报表")
    public ApiResp download(FoOrderMainReq foOrderMainReq, HttpServletResponse response){
        List<FoOrderMainDownloadResp> download = foOrderMainService.download(foOrderMainReq);
        try {
            ExcelUtils.exportExcel(download, "代付汇出报表", "代付报表", FoOrderMainDownloadResp.class, "FO-", response);
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusinessException("汇出报表错误");
        }
        return ApiResp.sucess();
    }
}
